package com.uchengwang.ui.cm_ui.cycle.ui;

import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.alivc.player.VcPlayerLog;
import com.aliyun.vodplayer.media.AliyunVidSts;
import com.aliyun.vodplayer.media.IAliyunVodPlayer;
import com.aliyun.vodplayerview.widget.AliyunVodPlayerView;
import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.listener.OnHttpResultSingleListener;
import com.uchengwang.ui.base_ui.ui.BaseNoTitleActivity;
import com.uchengwang.ui.cm_ui.release.bean.AliyunSTSTokenBean;
import com.uchengwang.ui.cm_ui.release.utils.VideoManagerUtils;

/**
 * Created by U城网 on 2018/5/3.
 * 阿里云短视频播放器
 */

public class ALiVideoPlayerActivity extends BaseNoTitleActivity{

//    private AliyunVodPlayer aliyunVodPlayer;
    private String videoId;

    private AliyunVodPlayerView playerView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_video_player);

        playerView = (AliyunVodPlayerView) findViewById(R.id.play_view);

        String sdDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ali_save_cache";
        //比如：maxSize设置500M时缓存文件超过500M后会优先覆盖最早缓存的文件。maxDuration设置为300秒时表示超过300秒的视频不会启用缓存功能。
        playerView.setPlayingCache(true, sdDir, 60 * 60 , 500 );

        videoId = getIntent().getStringExtra(BaseConfig.VIDEO_ID);
        Log.d("jjf", "视频id=="+videoId);
        initData();

    }

    private void initData(){
//        aliyunVodPlayer = new AliyunVodPlayer(this);

        //设置自动播放，设置后调用prepare之后会自动开始播放，无需调start接口
        playerView.setAutoPlay(true);
        //设置播放器音量（系统音量），值为0~100
//        aliyunVodPlayer.setVolume(30);
        //设置为静音
//        playerView.setMuteMode(false);
        //设置亮度（系统亮度），值为0~100
//        aliyunVodPlayer.setScreenBrightness(75);
        //设置显示模式，可设置为fit方式填充或corp方式裁剪充满
        playerView.setVideoScalingMode(IAliyunVodPlayer.VideoScalingMode.VIDEO_SCALING_MODE_SCALE_TO_FIT);
//        aliyunVodPlayer.setVideoScalingMode(IAliyunVodPlayer.VideoScalingMode.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);

        //获取媒体信息,在prepareDone的时候才可以获取，主要可获取信息有视频ID、视频标题、视频封面、视频总时长、当前视频清晰度、所有视频清晰度
//        aliyunVodPlayer.getMediaInfo();


        VideoManagerUtils.getUtils().getVideoSTS(this, null, new OnHttpResultSingleListener<AliyunSTSTokenBean>() {
            @Override
            public void onResult(int status, String msg, AliyunSTSTokenBean bean) {
                if (bean != null ){
                    AliyunVidSts mVidSts = new AliyunVidSts();
                    mVidSts.setVid(videoId);
                    mVidSts.setAcId(bean.getAccessKeyId());
                    mVidSts.setAkSceret(bean.getAccessKeySecret());
                    mVidSts.setSecurityToken(bean.getSecurityToken());
                    playerView.setVidSts(mVidSts);

                    //开始播放
                    playerView.start();
                }
            }
        });
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Log.d("lfj1019", " orientation = " + getResources().getConfiguration().orientation);
        updatePlayerViewMode();
    }

    private void updatePlayerViewMode() {
        if (playerView != null) {
            int orientation = getResources().getConfiguration().orientation;
            if (orientation == Configuration.ORIENTATION_PORTRAIT) {
                //转为竖屏了。
                //显示状态栏
//                if (!isStrangePhone()) {
//                    getSupportActionBar().show();
//                }

                this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

                playerView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);

                //设置view的布局，宽高之类
                LinearLayout.LayoutParams aliVcVideoViewLayoutParams = (LinearLayout.LayoutParams) playerView.getLayoutParams();
                aliVcVideoViewLayoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
                aliVcVideoViewLayoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
//                if (!isStrangePhone()) {
//                    aliVcVideoViewLayoutParams.topMargin = getSupportActionBar().getHeight();
//                }

            } else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {                //转到横屏了。
                //隐藏状态栏
                if (!isStrangePhone()) {
                    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
                    playerView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
                }

                //设置view的布局，宽高
                LinearLayout.LayoutParams aliVcVideoViewLayoutParams = (LinearLayout.LayoutParams) playerView.getLayoutParams();
                aliVcVideoViewLayoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
                aliVcVideoViewLayoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
//                if (!isStrangePhone()) {
//                    aliVcVideoViewLayoutParams.topMargin = 0;
//                }

            }

        }
    }

    private boolean isStrangePhone() {
        boolean strangePhone = Build.DEVICE.equalsIgnoreCase("mx5")
                || Build.DEVICE.equalsIgnoreCase("Redmi Note2")
                || Build.DEVICE.equalsIgnoreCase("Z00A_1")
                || Build.DEVICE.equalsIgnoreCase("hwH60-L02")
                || Build.DEVICE.equalsIgnoreCase("hermes")
                || (Build.DEVICE.equalsIgnoreCase("V4") && Build.MANUFACTURER.equalsIgnoreCase("Meitu"))
                || (Build.DEVICE.equalsIgnoreCase("m1metal") && Build.MANUFACTURER.equalsIgnoreCase("Meizu"));

        VcPlayerLog.e("lfj1115 ", " Build.Device = " + Build.DEVICE + " , isStrange = " + strangePhone);
        return strangePhone;

    }

    @Override
    protected void onResume() {
        super.onResume();

        if (playerView != null) {
            playerView.onResume();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (playerView != null) {
            playerView.onStop();
        }
    }

    @Override
    protected void onDestroy() {
        if (playerView != null) {
            playerView.onDestroy();
            playerView = null;
        }

        super.onDestroy();


    }
}
